package com.sky.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.sky.context.ThreadLocalUtil;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {


    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    // 查询购物车列表
    @Override
    public List<ShoppingCart> getList() {
        // 1.取出登录人
        Long userId = ThreadLocalUtil.getCurrentId();
        // 2.调用mapper查询
        return shoppingCartMapper.getByUserId(userId);
    }

    //添加购物车
    @Override
    public void save(ShoppingCartDTO shoppingCartDTO) {
        //dto->entity
        ShoppingCart shoppingCart = BeanUtil.copyProperties(shoppingCartDTO, ShoppingCart.class);
        //设置用户id
        shoppingCart.setUserId(ThreadLocalUtil.getCurrentId());
        //查询是否有此购物项
        ShoppingCart oldShoppingCart = shoppingCartMapper.getOne(shoppingCart);
        if (oldShoppingCart != null) {
            //存在,修改数量
            oldShoppingCart.setNumber(oldShoppingCart.getNumber() + 1);
            shoppingCartMapper.updateNumber(oldShoppingCart);
        } else {
            //不存在,新增
            //菜品新增,代码补全
            if (shoppingCart.getDishId() != null) {
                Dish dish = dishMapper.getById(shoppingCart.getDishId());
                shoppingCart.setName(dish.getName()); // 菜名
                shoppingCart.setImage(dish.getImage());// 图片
                shoppingCart.setAmount(dish.getPrice());// 单价
            }
            //套餐新增,代码补全
            else {
                Setmeal setmeal = setmealMapper.getById(shoppingCart.getSetmealId());
                shoppingCart.setName(setmeal.getName()); // 菜名
                shoppingCart.setImage(setmeal.getImage());// 图片
                shoppingCart.setAmount(setmeal.getPrice());// 单价
            }
            shoppingCart.setNumber(1); // 数量1
            shoppingCart.setCreateTime(LocalDateTime.now()); // 添加时间
            //添加
            shoppingCartMapper.insert(shoppingCart);
        }

    }

    //清空购物车
    @Override
    public void clean() {
        shoppingCartMapper.clean(ThreadLocalUtil.getCurrentId());
    }

    //删除购物项
    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        //设置查询条件，查询当前购物项
        ShoppingCart shoppingCart = BeanUtil.copyProperties(shoppingCartDTO, ShoppingCart.class);
        shoppingCart.setUserId(ThreadLocalUtil.getCurrentId());
        ShoppingCart oldShoppingCart = shoppingCartMapper.getOne(shoppingCart);

        // 删除购物项或者数量-1
        if (oldShoppingCart != null) {
            Integer number = oldShoppingCart.getNumber();
            if (number == 1) {
                //当前商品在购物车中的份数为1，直接删除当前记录
                shoppingCartMapper.deleteById(oldShoppingCart.getId());
            } else {
                //当前商品在购物车中的份数不为1，修改份数即可
                oldShoppingCart.setNumber(oldShoppingCart.getNumber() - 1);
                shoppingCartMapper.updateNumber(oldShoppingCart);
            }
        }

    }
}
